package com.ymatou.shop.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.ui.activity.buyer.PayOrderActivity;
import com.ymatou.shop.ui.view.ImageFragment;
import com.ymatou.shop.ui.view.PageIndicator;
import com.ymatou.shop.ui.view.CirclePageIndicator;
import com.ymatou.shop.util.GlobalUtil;
import com.ymatou.shop.util.Utils;

public class HomeActivity extends BaseWithLoginActivity implements OnClickListener, OnPageChangeListener {

	private static final int REQUEST_ID_HOME_LIST = 0x00;

	private ImageFragmentAdapter mAdapter;
	private ViewPager mPager;
	private PageIndicator mIndicator;
	private List<Product> mProductList;
	private int mPosition;
	private TextView productInfoTextView, moreTextView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_activity);

		setupViews();

		setupProductData();
	}

	@Override
	protected void setupViews() {
		// TODO Auto-generated method stub
		super.setupViews();


		getTitleRightImageButton1().setVisibility(View.VISIBLE);
		getTitleRightImageButton1().setOnClickListener(this);
		getTitleRightImageButton1().setImageResource(R.drawable.refresh_icon);
		getTitleTextView().setText(getString(R.string.tab_home));
		changeLoadingViewState(false);

		productInfoTextView = (TextView) findViewById(R.id.title_textview);
		productInfoTextView.setOnClickListener(this);
		moreTextView = (TextView) findViewById(R.id.more_textview);
		moreTextView.setOnClickListener(this);

		mProductList = new ArrayList<Product>();

		mAdapter = new ImageFragmentAdapter(getSupportFragmentManager());
		mAdapter.setCount(mProductList.size());
		
		mPager = (ViewPager) findViewById(R.id.pager);
	

		
		mPager.setAdapter(mAdapter);
		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mIndicator.setOnPageChangeListener(this);
		mIndicator.setViewPager(mPager);
	}

	private void setupProductData() {
		RequestInfo requestInfo = ApiRequest.getHomeList();
		getCacheManager().register(REQUEST_ID_HOME_LIST, requestInfo, this);
	}


	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo, Object response) {
		super.onCacheSuccess(requestId, requestInfo, response);
		if (requestId == REQUEST_ID_HOME_LIST) {
			mProductList = ApiResponse.getHomeList((String) response);
			showProductData();
			changeLoadingViewState(true);
		}

	}

	public void showProductData() {
		productInfoTextView.setText(mProductList.get(0).mHomeDescription);
		mAdapter.setCount(mProductList.size());
	}

	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo, CacheException error) {
		cancelProgressDialog();
		super.onCacheFailed(requestId, requestInfo, error);
	}

	public class ImageFragmentAdapter extends FragmentPagerAdapter {

		private int mCount;

		public ImageFragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			return ImageFragment.newInstance(mProductList.get(position), getApplicationContext());
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			if (position >= getCount()) {
				FragmentManager manager = ((Fragment) object).getFragmentManager();
				FragmentTransaction trans = manager.beginTransaction();
				trans.remove((Fragment) object);
				trans.commit();
			}
		}

		@Override
		public int getCount() {
			return mCount;
		}

		public void setCount(int count) {
			if (count > 0) {
				mCount = count;
				notifyDataSetChanged();
			}
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		//	  super.onSaveInstanceState(outState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_right_image_button1:
			setupProductData();
			break;
		case R.id.title_textview:

			break;
		case R.id.more_textview:
			if (mPosition >= 0 && mProductList != null && mProductList.size() > mPosition) {
				GlobalUtil.startEventListActivity(this, mProductList.get(mPosition).mSellerId);
			}
			break;
		default:
			break;
		}
		super.onClick(v);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		productInfoTextView.setText(mProductList.get(position).mHomeDescription);
		mPosition = position;
	}
}